import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { AuthDictService } from '../../../../system/dict/service/dict.service';
import { TransportService } from '../../../transport-service/transport.service';
import { TransferStatService } from '../transfer-stat.service';
import { SearchParamUtils } from '../../../../../shared/utils/SearchParamUtils';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class TransferStatListComponent implements OnInit {
	searchForm: FormGroup;
	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};
	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.st.pi - 1) * this.st.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '日期班次',
			index: 'timeInfo',
			width: 80
		},
		{
			title: '船名',
			index: 'oriShipInfo',
			width: 80
		},
		{
			title: '单号',
			index: 'oriBillNo',
			width: 80
		},
		{
			title: '货名',
			index: 'oriBillCargoTypeName',
			width: 80
		},
		{
			title: '包装',
			index: 'oriPackageTypeName',
			width: 80
		},
		{
			title: '位置',
			index: 'positionInfo',
			width: 80
		},
		{
			title: '件吨',
			index: 'oriPieceTonInfo',
			width: 80
		},
		{
			title: '转入船',
			index: 'toShipInfo',
			width: 80
		},
		{
			title: '转入舱单',
			index: 'toBillNo',
			width: 80
		},
		{
			title: '理货员',
			index: 'createUserName',
			width: 80
		}
	];
	dataList = [];
	totalData: any;
	timeSpaceList = [];
	userList = [];
	shipList = [];
	billList= [];
	toShipList = [];
	toBillList= [];
	dateRanges = environment.dateRanges;
	constructor(
		private fb: FormBuilder,
		private dictService: AuthDictService,
		private transferService: TransferStatService,
		private transportService: TransportService
	) {
		this.userList = this.dictService.tallyUserList;
		this.transportService.transportShipList({}).subscribe((res:any) => {
			this.shipList = res;
			this.toShipList = res;
		});
		this.dictService.dictionary(environment.dict.TIME_SPACE, 1).subscribe(res => {
			this.timeSpaceList = res;
		});
		this.searchForm = this.fb.group({
			startAndEndTime: null,
			timeSpace: null,
			createUser: null,
			transportShipId: null,
			transportBillId: null,
			toTransportShipId: null,
			toTransportBillId: null
		});
	}

	ngOnInit(): void {
		this.loadData();
	}

	loadData(): any {
		this.pageInfo.loading = true;
		const params = {
			...this.pageInfo,
			...this.searchForm.value
		};

		const formatter = new SearchParamUtils();
		const startAndEndTimeFormatted = formatter.dateRangePickerFormat(params?.startAndEndTime);
		params.startTime = startAndEndTimeFormatted[0];
		params.endTime = startAndEndTimeFormatted[1];

		this.transferService.transferStatPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
			this.transferService.transferStatTotal(params).subscribe((total: any) => {
				this.totalData = total;
			});
		});
	}

	search(): any {
		this.pageInfo.current = 1;
		this.loadData();
	}

	reset() {
		this.pageInfo.current = 1;
		this.searchForm.reset();
		this.loadData();
	}

	stChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

	searchBillList(data: any, target: string) {
		if (data) {
			this.transportService.transportBillList({ transportShipId: data }).subscribe(res => {
				if (target === 'ori') {
					this.billList = res;
				}
				if (target === 'to') {
					this.toBillList = res;
				}
			});
		}
	}

	searchShipList(data: string, target: string) {
		this.transportService.transportShipList({ keyword: data }).subscribe(res => {
			this.shipList = res;
			if (target === 'ori') {
				this.shipList = res;
			}
			if (target === 'to') {
				this.toShipList = res;
			}
		});
	}
}
